package com.ctbri.manage.compute.scala

import org.apache.spark.sql.SparkSession
import org.slf4j.{Logger, LoggerFactory}

/**
 * @Author wangxuem
 * @create 2023/6/18 19:02
 */
object WordCount {
  protected final val logger: Logger = LoggerFactory.getLogger(this.getClass)

  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      //      .master("local[*]")
      .appName("WordCount")
      .getOrCreate()
    if (args.length == 0) {
      logger.info("args length is 0 ")
      System.exit(1)
    }
    val result = spark.sparkContext.textFile(args(0))
      .flatMap(_.split(" "))
      .map((_, 1))
      .reduceByKey(_ + _)
    result.collect().foreach(println)
    spark.close()
    logger.info("Task finish!!")
  }
}
